package com.cskaoyan;

import com.cskaoyan.bean.Teacher;
import com.cskaoyan.mapper.TeacherMapper;
import com.cskaoyan.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

/**
 * 创建日期: 2022/08/01 15:22
 *
 * @author ciggar
 */
public class MybatisMain {

    public static void main(String[] args) {

        // 获取SqlSession
        SqlSession sqlSession = MybatisUtils.getSqlSession(true);

        // 获取接口的实例对象（获取的是代理对象：MapperProxy）//Proxy.newInstance
        TeacherMapper teacherMapper = sqlSession.getMapper(TeacherMapper.class);

        // 执行方法，执行对应的SQL语句
        Teacher teacher = teacherMapper.selectTeacherById(3001);

        System.out.println(teacher);


    }

    public static void main2(String[] args){

        SqlSession sqlSession = MybatisUtils.getSqlSession(true);

        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        Teacher teacher = new Teacher();
        teacher.setId(3001);
        teacher.setName("尼古拉斯赵四");
        teacher.setGender("male");
        teacher.setNickname("亚洲舞王");
        int affectedRows = mapper.insertTeacher(teacher);

        System.out.println(affectedRows);


    }


    public static void main3(String[] args){

        SqlSession sqlSession = MybatisUtils.getSqlSession(true);

        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        int affectedRows = mapper.deleteTeacherById(2001);

        System.out.println(affectedRows);

    }

    public static void main4(String[] args){

        SqlSession sqlSession = MybatisUtils.getSqlSession(true);

        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        Teacher teacher = new Teacher();
        teacher.setId(3001);
        teacher.setName("沈腾");
        teacher.setNickname("来个98的");

        int affectedRows = mapper.updateTeacherNameAndNicknameById(teacher);

        System.out.println(affectedRows);

    }

    public static void main5(String[] args) {

        SqlSession sqlSession = MybatisUtils.getSqlSession(true);

        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        String name = mapper.selectNameById(3001);

        System.out.println("name：" + name);

    }


}
